<?php

namespace plugin\admin\app\controller;

use plugin\admin\app\model\Goods;
use plugin\admin\app\model\PrizeDrawActivity;
use support\Request;
use support\Response;
use plugin\admin\app\model\PrizeDrawActivityGood;
use plugin\admin\app\controller\Crud;
use support\exception\BusinessException;

/**
 * 抽奖商品 
 */
class PrizeDrawActivityGoodController extends Crud
{
    
    /**
     * @var PrizeDrawActivityGood
     */
    protected $model = null;

    /**
     * 构造函数
     * @return void
     */
    public function __construct()
    {
        $this->model = new PrizeDrawActivityGood;
    }
    
    /**
     * 浏览
     * @return Response
     */
    public function index(Request $request): Response
    {
        return view('prize-draw-activity-good/index', [
            'id' => $request->get('id')
        ]);
    }

    public function delete(Request $request): Response
    {
        $ids = $this->deleteInput($request);

        $good = PrizeDrawActivityGood::where('id', $ids[0])->first();

        $this->doDelete($ids);

        if (count($ids) > 0) {
            $this->updateActivity($good['prize_draw_activity_id']);
        }

        return $this->json(0);
    }

    public function insert(Request $request): Response
    {
        if ($request->method() === 'POST') {
            $data = $this->insertInput($request);

            if (PrizeDrawActivityGood::where('prize_draw_activity_id', $data['prize_draw_activity_id'])->where('goods_id', $data['goods_id'])->exists()) {
                return $this->json(400, '该商品已存在');
            }

            $id = $this->doInsert($data);
            $this->updateActivity($data['prize_draw_activity_id']);
            return $this->json(0, 'ok', ['id' => $id]);
        }
        return view('prize-draw-activity-good/insert', [
            'id' => $request->get('id')
        ]);
    }

    /**
     * 更新
     * @param Request $request
     * @return Response
     * @throws BusinessException
    */
    public function update(Request $request): Response
    {
        if ($request->method() === 'POST') {
            return parent::update($request);
        }
        return view('prize-draw-activity-good/update');
    }

    protected function formatNormal($items, $total): Response
    {
        $goodsIds = array_column($items, 'goods_id');
        $goodsList = Goods::whereIn('id', $goodsIds)->get()->keyBy('id')->toArray();

        foreach ($items as $key=>$item) {
            $goods = $goodsList[$item['goods_id']] ?? [];
            $item['goods'] = $goods;

            $items[$key] = $item;
        }

        return json(['code' => 0, 'msg' => 'ok', 'count' => $total, 'data' => $items]);
    }

    public function updateActivity($aid)
    {
        $count = PrizeDrawActivityGood::where('prize_draw_activity_id', $aid)->count();

        $type = PrizeDrawActivity::TYPE_SINGLE;
        if ($count > 1) {
            $type = PrizeDrawActivity::TYPE_MULTI;
        }

        print_r([$count, $type]);
        PrizeDrawActivity::where('id', $aid)->update(['type' => $type]);
    }

}
